home *** CD-ROM | disk | FTP | other *** search
- ' XLS_to_CSV_Single.vbs
- '
- ' Converts <input file> from an Excel workbook to a comma-separated text file
- ' and saves the results to <output file>. Requires Microsoft Excel.
- '
- ' Usage:
- ' WScript XLS_to_CSV_Single.vbs <input file> <output file>
-
- ' WdSaveFormat constants, taken from the Word type library
- Const xlCSV = 6 ' Comma-separated values
- Const xlUnicodeText = 42 ' Tab delimited text file
-
- ' OpenTextFile iomode constants
- Const ForReading = 1
- Const ForAppending = 8
-
- Dim oFileSys
- Dim oExcel
- Dim sTmpName
- Dim oDesFile, oTmpFile
-
- Set oFileSys = CreateObject("Scripting.FileSystemObject")
- oFileSys.DeleteFile WScript.Arguments(1)
-
- Set oExcel = CreateObject("Excel.Application")
-
- On Error Resume Next
-
- oExcel.Workbooks.Open WScript.Arguments(0), , True
- If Err = 0 Then
- With oExcel.ActiveWorkbook
- If .Sheets.Count = 1 Then
- .SaveAs WScript.Arguments(1), xlCSV
- Else
- Set oDesFile = oFileSys.OpenTextFile( WScript.Arguments(1), ForAppending, True)
- .Sheets(0).Activate
- sTmpName = oFileSys.GetSpecialFolder(2) & "\" & oFileSys.GetTempName
- .SaveAs sTmpName, xlCSV
- Set oTmpFile = oFileSys.OpenTextFile( sTmpName, ForReading )
- oDesFile.Write( oTmpFile.ReadAll )
- oTmpFile.Close
- oFileSys.DeleteFile sTmpName
- End If
- .Close False
- End With
- End If
-
- oExcel.Quit
- If (Err <> 0) and (Err.Description <> NULL) Then
- MsgBox Err.Description, vbExclamation, "Error"
- Err.Clear
- End If